<?php

class Utils{

    // keyword density calculator
    static function keywordDensityCalculator($key, $words)
    {
        $pattern = '/\b' . $key . '\b/'; 
        preg_match_all($pattern, $words, $out, PREG_PATTERN_ORDER);
        return (count($out[0])*100)/self::wordsCounter($words);
    }
    
    // count all words in a text
    static function wordsCounter($words)
    {
        $aWords = split("[][ ,.;:?!\'\"{()}]", $words);
        return count($aWords);
    }

    // return ip address of the site
    static function getIp($url)
    {
        return gethostbyname($url);
    }

    // return user agent
    static function getUserAgent()
    {
        //return $_SERVER['HTTP_USER_AGENT'];
        return get_browser(null, true);
    }

    // return header response from http request
    static function getHeaderResponse($url)
    {
        $ch = curl_init();
        $header = array();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        curl_setopt($ch, CURLOPT_FILETIME, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        
        // Set callback function for headers
        curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'self::read_header');
        // Set callback function for body
        curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'self::read_body');

        curl_exec($ch);
        
        if ($error = curl_error($ch)) 
        {
            echo "Error: $error<br />\n";
            curl_close($ch);
            return;
        }

        $header = curl_getinfo($ch);
        return $header;
        
    }

    static function read_header($ch, $string)
    {
        $length = strlen($string);
        return $length;
    }

    static function read_body($ch, $string)
    {
        $length = strlen($string);
        return $length;
    }

    // check if exist robots.txt
    static function checkRobots($url)
    {
        $parsed = $url;
        $sitemapFile = @file("http://{$parsed}/robots.txt");
        if(!$sitemapFile) return false;
        return true;        
    }

    // return robots.txt content
    static function getRobots($url)
    {
        $parsed = $url;
        $sitemapFile = file_get_contents("http://{$parsed}/robots.txt");
        if(!$sitemapFile) return false;
        return $sitemapFile;
    }

    // check if robots.txt exist and is correct
    static function robots_allowed($url, $useragent=false) 
    {   
        $robotstxt = false;
        # parse url to retrieve host and path 
        //$parsed = parse_url($url);  
        //var_dump($parsed);exit;
        $parsed = $url;
        $agents = array(preg_quote('*')); 
        if($useragent) $agents[] = preg_quote($useragent); 
        $agents = implode('|', $agents); 
        # location of robots.txt file 
        //$robotstxt = @file("http://{$parsed['host']}/robots.txt"); 
        $robotstxt = @file("http://{$parsed}/robots.txt"); 
        if(!$robotstxt) return false; 
        $rules = array(); 
        $ruleapplies = false; 
        foreach($robotstxt as $line) { 
            # skip blank lines 
            if(!$line = trim($line)) continue; 
            # following rules only apply if User-agent matches $useragent or '*' 
            if(preg_match('/User-agent: (.*)/i', $line, $match)) 
            { 
                $ruleapplies = preg_match("/($agents)/i", $match[1]); 
            } 
            if($ruleapplies && preg_match('/Disallow:(.*)/i', $line, $regs)) 
            { 
            # an empty rule implies full access - no further tests required 
                if(!$regs[1]) return true; 
                # add rules that apply to array for testing 
                $rules[] = preg_quote(trim($regs[1]), '/'); 
            } 
        } 
        foreach($rules as $rule) 
        { 
        # check if page is disallowed to us 
            if(preg_match("/^$rule/", $parsed['path'])) return false; 
        } 
        # page is not disallowed 
        return true; 
    } 

    // check if exist sitemap.xml 
    static function checkSitemap($url)
    {
        $parsed = $url;
        $sitemapFile = @file("http://{$parsed}/sitemap.xml");
        if(!$sitemapFile) return false;
        return true;
    }

    // return che sitemap.xml content
    static function getSitemap($url)
    {
        $parsed = $url;
        $sitemapFile = file_get_contents("http://{$parsed}/sitemap.xml");
        if(!$sitemapFile) return false;
        return $sitemapFile;
    }



}
